using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WaterFlowDemo
{
    public partial class MainWindow : Window
    {
        private bool isFlowingLeftToRight = true;
        private Storyboard waterFlowStoryboard;
        private Path pipe;
        private Path waterFlow;

        public MainWindow()
        {
            InitializeComponent();
            CreatePipe();
            InitializeWaterFlowAnimation();
        }

        private void CreatePipe()
        {
            // 创建管道
            pipe = new Path
            {
                Stroke = Brushes.Red,
                StrokeThickness = 10
            };

            var pipeGeometry = new PathGeometry();
            var pipeFigure = new PathFigure
            {
                StartPoint = new Point(150, 250)
            };

            // 添加直线和折线段
            pipeFigure.Segments.Add(new LineSegment(new Point(200, 250), true));
            pipeFigure.Segments.Add(new LineSegment(new Point(250, 200), true));
            pipeFigure.Segments.Add(new LineSegment(new Point(300, 200), true));
            pipeFigure.Segments.Add(new LineSegment(new Point(350, 300), true));
            pipeFigure.Segments.Add(new LineSegment(new Point(400, 300), true));
            pipeFigure.Segments.Add(new LineSegment(new Point(450, 250), true));
            pipeFigure.Segments.Add(new LineSegment(new Point(550, 250), true));

            pipeGeometry.Figures.Add(pipeFigure);
            pipe.Data = pipeGeometry;

            // 创建水流动画路径
            waterFlow = new Path
            {
                Stroke = Brushes.Blue,
                StrokeThickness = 5,
                StrokeDashArray = new DoubleCollection { 10, 5 }
            };

            // 使用相同的几何图形
            waterFlow.Data = pipeGeometry.Clone();

            // 添加到画布
            MainCanvas.Children.Add(pipe);
            MainCanvas.Children.Add(waterFlow);
        }

        private void InitializeWaterFlowAnimation()
        {
            // 创建水流动画
            waterFlowStoryboard = new Storyboard();
            var animation = new DoubleAnimation
            {
                From = 0,
                To = 15,
                Duration = TimeSpan.FromSeconds(1),
                RepeatBehavior = RepeatBehavior.Forever
            };

            Storyboard.SetTarget(animation, waterFlow);
            Storyboard.SetTargetProperty(animation, new PropertyPath("(Path.StrokeDashOffset)"));

            waterFlowStoryboard.Children.Add(animation);
            waterFlowStoryboard.Begin(this);
        }

        private void ToggleFlowButton_Click(object sender, RoutedEventArgs e)
        {
            isFlowingLeftToRight = !isFlowingLeftToRight;
            waterFlowStoryboard.Stop();

            // 反转动画方向
            var animation = new DoubleAnimation
            {
                From = isFlowingLeftToRight ? 0 : 15,
                To = isFlowingLeftToRight ? 15 : 0,
                Duration = TimeSpan.FromSeconds(1),
                RepeatBehavior = RepeatBehavior.Forever
            };

            Storyboard.SetTarget(animation, waterFlow);
            Storyboard.SetTargetProperty(animation, new PropertyPath("(Path.StrokeDashOffset)"));

            waterFlowStoryboard.Children.Clear();
            waterFlowStoryboard.Children.Add(animation);
            waterFlowStoryboard.Begin(this);
        }
    }
} 